<?php
namespace app\admin\controller;
use think\View;
use think\Cache;

class Base extends \think\Controller
{
  	public function __construct()
	{
	    parent::__construct();
		
		//栏目循环
		$term_info = D('term')->selectAll();
		$fen_ji = $this->getTermTree(0,$term_info);
		$this->view->assign('term_info',$fen_ji);
		
		$select_all_parent_id = M('term')->field('parent_id')->select();
		if($select_all_parent_id){
			
		foreach( $select_all_parent_id as $key=>$value ){
        $result[] = $value['parent_id'];
        }
		$this->view->assign('result',$result);
		}
		
		
		
	}
	
    //废弃
	public function uploads(){
		
		 if (!empty($_FILES)) {
			 $upload = new \org\upload();
			 $upload->maxSize  = 3145728 ;
			 $upload->saveRule = 'time';
			 $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','zip','rar','doc','docx');
			 $upload->savePath = "public/Uploads/";
			 
			 if(!$upload->upload()) 
			 {
			 $this->error($upload->getErrorMsg());
			 }else{
				 $info =  $upload->getUploadFileInfo();
				 // foreach($info as $key => $val){
					// file_put_contents('infotest.json',$val."\n\r");
				 // }
				 echo json_encode($info[0]);
				 exit();
			 }
			 

		 }else {
			 echo "上传失败";
		 }
		
    } 
	
	
	protected  function get($parentId = 0, $array = array(), $level = 0, $add = 2, $repeat = '&nbsp;&nbsp;')
    {
        $strRepeat = '';
        if ($level>1) {
            for($j = 0; $j < $level; $j ++)
            {
                $strRepeat .= $repeat;
            }
        }
        if($level>0)
            $strRepeat .= '';
        $newArray = array ();
        $tempArray = array ();
        foreach ( ( array ) $array as $v )
        {
            if ($v['parent_id'] == $parentId)
            {
                $newArray [] = array ('id' => $v['id'], 'name' => $v['name'], 'parent_id' => $v['parent_id'],  'str_label' => $strRepeat.$v['name'],'status'=>$v['status'],'display_nav'=>$v['display_nav'],'type'=>$v['type'],'listorder'=>$v['listorder'],'id'=>$v['id'],'top_ok'=>$v['top_ok'],'rec_ok'=>$v['rec_ok']);

                $tempArray = self::get ( $v['id'], $array, ($level + $add), $add, $repeat);
                if ($tempArray)
                {
                    $newArray = array_merge ( $newArray, $tempArray );
                }
            }
        }
        return $newArray;
    }

    /**
     * 重构生成树的方法
     * @param int $topId 顶级父栏目ID
     * @param array $termArr  栏目数组
     * @param str $repeat 每层添加的前缀
     * 马宇翔
     */
    protected function getTermTree($topId = 0, $termArr = [], $repeat = '||&nbsp;&nbsp;&nbsp;&nbsp;')
    {
        if(empty($termArr)) return [];
        $newTermArr = $childArr = [];
        foreach($termArr as $termInfo)
            $childArr[$termInfo['parent_id']][] = $termInfo;
        $funGet = function ($id, $strRepeat) use (&$newTermArr, $childArr, $repeat, &$funGet) {
            foreach($childArr[$id] as $termInfo){
                $termInfo['name'] = $strRepeat.$termInfo['name'];
                $termInfo['hasChild']= isset($childArr[$termInfo['id']])? 1: 0;
                $newTermArr[] = $termInfo;
                if($termInfo['hasChild'])
                    $funGet($termInfo['id'], $termInfo['name'].'->');
            }
        };
        $funGet($topId, '');//闭包引用传入$newTermArr
        return $newTermArr;
    }
	
    protected function isLogin()
    {
		
        if(false == session('user') || time()-session('lastAct')>C('login_timeout')){
            $this->redirect('Sign/in');
			exit();
        }
        session('lastAct', time());
    }

    /**
     * @param $content  确认信息的内容
     * @param $flag     确认参数标识
     * @param $keyfalg  PostKey参数标识
     * @result bool
     */
    protected function reconfirm($msg='', $flag='reconfirm', $keyflag='rcfkey')
    {
        $reconfirm = I('get.'.$flag);
        if($reconfirm === '0')
            return false;
        if($reconfirm === '1'){
            $key = I('get.'.$keyflag);
            $server = unserialize(cookie($key.'server'));
            $_SERVER = array_merge($_SERVER, $server);
            $_POST = unserialize(cookie($key.'post'));
            return true;
        }
        $vals = I('get.');
        $key = md5(time().'reconfirm');
        cookie(['expire'=>600]);
        if(!isset($_SERVER['HTTP_REFERER']))
            $this->error('some thing is wrong!', U('index/index'));
        $server['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
        cookie($key.'server', serialize($server));
        cookie($key.'post', serialize($_POST));
        $vals = array_merge($vals, [$keyflag=>$key]);
        $this->assign('vals', $vals);
        $this->assign('flag', $flag);
        $this->assign('msg', $msg);
        $tpl = C('dispatch_reconfirm_tmpl');
        $tpl = $tpl? $tpl: 'public/reconfirm';
        echo $this->fetch($tpl);
        exit;
    }
	
	/**
     * @param $count  传入所取信息个数
     * @param $pagecount  每页个数
     * @param $page  模板调用
     * Author: yanli
     */
	protected function getPage($count,$pagecount=2)
	{
		$getPage = new \org\Page($count,$pagecount);
		$theme = "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%";
		$getPage->setConfig('theme',$theme);
		$page = $getPage->show();
		$this->view->assign('page',$page);
		return $getPage;
	}
	
	/**
     * @param  namespace org;
     * Author: yanli
     */
    public function verify()
	{
		$config =    array(
			'fontSize'    =>    14,    // 验证码字体大小
			'length'      =>    4,     // 验证码位数
			'useNoise'    =>    false, // 关闭验证码杂点
			'codeSet' =>'0123456789',
             ); 
		$Verify = new \org\Verify($config);
		ob_end_clean();
		$Verify->entry();   
	}
	
    /**
     * @param  namespace org;
     * @param  一键清理缓存;
     * Author: yanli
     */
	 
	 public function clear_cache()
	 {
      if(Cache::clear()){
		return $this->success('清除缓存成功', U('Index/index')); 
	  }
        
	 }
	

			
	
	
	
	
	
	
	
	
	
}

